﻿using Glorysoft.Auto.Xfw.Entities;
using Glorysoft.EAP.Contracts.MESClient.Models;
using Glorysoft.EAP.Core;
using Glorysoft.EAP.Core.Entities;
using Glorysoft.EAP.Core.Enums;
using System;
using System.Collections.Generic;

namespace Glorysoft.EAP.Contracts.MESClient
{
    public class MESClient
    {
        /// <summary>
        /// 根据三码获取批次详细信息
        /// </summary>
        /// <param name="para"></param>
        /// <param name="rcLot"></param>
        /// <returns></returns>
        public Return GetLotInfo(TagObject para, ref LotInfo rcLot)
        {
            Return _return = new Return();
            try
            {
                var request = new RequestLotInfo();
                request.LOTID = para.LotId;
                request.CARRIERNAME = para.CarrierName;
                request.EQUIPMENTID = para.EqpId;
                request.TOOLLIST = new List<Models.Tool>();
                foreach (var item in para.TooList)
                {
                    var tool = new Models.Tool();
                    tool.TOOLID = item.TOOLID;
                    tool.TOOLQTY = item.TOOLQTY;
                    request.TOOLLIST.Add(tool);
                }
                var client = new SMECMESClient();
                var response = client.GetLotInfo(request, para.BarCodeMaterialId, para.BarCodeEqpId, para.UserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                {
                    _return.SetReturnOK();
                    rcLot.CARRIERLIST = response.CARRIERLIST;
                    rcLot.ITEMLIST = response.ITEMLIST;
                    rcLot.LotId = response.Lot.lotId;
                    rcLot.RecipeId = response.Lot.recipe;
                    rcLot.ProductId = response.Lot.productName;
                    rcLot.Qty = response.Lot.qty;
                    rcLot.LotStatus = "Unknow";
                    rcLot.RunStatus = ProcessState.GetLotInfoOK;
                    rcLot.LOSSINFOLIST = response.LOSSINFOLIST;
                    rcLot.UNITLIST = response.Lot.UNITLIST;
                    //rcLot.CarrierId = response.carrierName.ToString();
                    rcLot.CurrentCarreir.Units = new List<UnitInfo>();
                    //foreach (var unit in response.Lot.UnitList)
                    //{
                    //    var _unit = new UnitInfo();
                    //    _unit.UnitId = unit.unitId;
                    //    _unit.UnitStatus = unit.slot;
                    //    _unit.Position = unit.slot;
                    //    //_unit.ItemId = unit.;
                    //    _unit.RecipeId = unit.recipeId;
                    //    rcLot.CurrentCarreir.Units.Add(_unit);
                    //}

                }
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// 调取MES自动进站接口
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns> 
        public Return TrackIn(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var request = new RequestTrackIn();
                request.EQUIPMENTID = para.EqpId;
                request.CARRIERNAME = para.CarrierName;
                request.LOTID = para.LotId;
                request.RECIPE = para.RecipeId;
                request.TOOILIST = new List<TOOL>();
                foreach (var item in para.TooList)
                {
                    var tool = new TOOL();
                    tool.TOOLID = item.TOOLID;
                    tool.TOOLQTY = item.TOOLQTY;
                    request.TOOILIST.Add(tool);
                }
                var client = new SMECMESClient();
                var response = client.LotTrackIn(request, para.UserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// 调取MES 自动出站接口
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public Return TrackOut(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var request = new RequestTrackOut();
                request.EQUIPMENTID = para.EqpId;
                //request.CARRIERNAME = para.CarrierName;
                request.LOTID = para.LotId;
                request.TOOLIST = new List<ToolList>();
                request.CARRIERLIST = new List<CARRIERLIST>();
                request.LOSSINFOLIST = new List<LOSSINFOLIST>();
                foreach (var item in para.TooList)
                {
                    var tool = new ToolList();
                    tool.TOOLID = item.TOOLID;
                    tool.TOOLQTY = item.TOOLQTY;
                    request.TOOLIST.Add(tool);
                }
                foreach (var item in para.CARRIERLIST)
                {
                    var carrier = new CARRIERLIST();
                    carrier.CARRIERNAME = item.CARRIERNAME;
                    request.CARRIERLIST.Add(carrier);
                }
                foreach (var item in para.LOSSINFOLIST)
                {
                    List<Core.Entities.LOSSINFOLIST> Loss = new List<Core.Entities.LOSSINFOLIST>();
                    foreach (Core.Entities.LOSSINFOLIST item1 in para.LOSSINFOLIST)
                    {
                        if (item1.PIDLIST.Count > 0 || item1.LOSSCOUNT != null && int.Parse(item1.LOSSCOUNT) != 0)
                        {
                            Core.Entities.LOSSINFOLIST lossinfo = new Core.Entities.LOSSINFOLIST();
                            lossinfo.LOSSCODE = item1.LOSSCODE;
                            lossinfo.LOSSCOUNT = item1.LOSSCOUNT;
                            lossinfo.LOSSDESC = item1.LOSSDESC;
                            if (item1.PIDLIST.Count == 0)
                            {
                                lossinfo.PIDLIST = null;
                            }
                            else
                            {
                                lossinfo.PIDLIST = item1.PIDLIST;
                            }
                            Loss.Add(lossinfo);
                        }
                    }
                    request.LOSSINFOLIST = Loss;
                }
                var client = new SMECMESClient();
                var response = client.LotTrackOut(request, para.UserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// 特殊情况下需要EAP自动Hold批次
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public Return HoldLot(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var request = new RequestHoldLot();
                request.EQUIPMENTID = para.EqpId;
                request.LOTID = para.LotId;
                request.HOLDREASON = para.HoldReasons;
                var client = new SMECMESClient();
                var response = client.HoldLot(request, para.BarCodeUserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// 用户名密码验证
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public Return ConfirmLogin(TagObject para, string eqpID, string userid, string password)
        {
            Return _return = new Return();
            try
            {
                var reuqest = new RequestLogin();
                reuqest.EQUIPMENTID = eqpID;
                reuqest.USERNAME = userid;
                reuqest.PASSWORD = password;
                var client = new SMECMESClient();
                var response = client.ConfirmLogin(reuqest, userid);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// 上报设备警报信息
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public Return AlarmReport(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var request = new RequestAlarmReport();
                request.EQUIPMENTID = para.EqpId;
                request.ALARMCODE = para.AlarmCode;
                request.ALARMTEXT = para.AlarmText;
                request.ALARMTIME = para.AlarmTime;
                var client = new SMECMESClient();
                var response = client.AlarmReport(request, para.BarCodeUserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// 上料
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public Return KittingRequest(TagObject para, ref Kitting rcLot)
        {
            Return _return = new Return();
            try
            {
                var request = new RequestKittingRequest();
                request.EQUIPMENTID = para.EqpId;
                request.LOTID = para.LotId;
                request.MTID = para.MTID;
                var client = new SMECMESClient();
                var response = client.KittingRequest(request, para.UserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                {
                    _return.SetReturnOK();
                    rcLot.EQUIPMENTID = response.EQUIPMENTID;
                    rcLot.LOTID = response.LOTID;
                    rcLot.MTID = response.MTID;
                    rcLot.TYPE = response.TYPE;
                    rcLot.QTY = response.QTY;
                }
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        public Return UnKittingRequest(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var reuqest = new RequestUnKitting();
                reuqest.EQUIPMENTID = para.EqpId;
                reuqest.MTID = para.MTID;
                reuqest.USEDQTY = para.USEDQTY;
                var client = new SMECMESClient();
                var response = client.UnKittingRequest(reuqest, para.UserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// 作业过程中更换治具或者材料
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public Return ToolChange(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var reuqest = new RequestToolChange();
                reuqest.EQUIPMENTID = para.EqpId;
                var client = new SMECMESClient();
                var response = client.ToolChange(reuqest, para.BarCodeUserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// 与MES系统进行设备信息数据同步
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public Return EQPSynchronization(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var reuqest = new RequestEQPList();
                reuqest.EqpId = para.EqpId;
                var client = new SMECMESClient();
                var response = client.EQPSynchronization(reuqest);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// 采集数据上报MES 
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public Return EDCDataReport(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var request = new RequestEDCDataReport();
                request.EQUIPMENTID = para.EqpId;
                request.CARRIERNAME = para.CarrierName;
                request.SUBUNITNAME = para.SubUnitName;
                request.LOTID = para.LotId;
                request.ITEMLIST = new List<Models.Item>();
                foreach (var localItem in para.ItemList)
                {
                    var item = new Models.Item();
                    item.ITEMNAME = localItem.ItemName;
                    item.SITELIST = new List<Models.Site>();
                    foreach (var localSite in localItem.SITELIST)
                    {
                        var site = new Models.Site();
                        site.SITENAME = localSite.SiteName;
                        site.SITEVALUE = localSite.SiteValue;
                        item.SITELIST.Add(site);
                    }
                    request.ITEMLIST.Add(item);
                }
                var client = new SMECMESClient();
                var response = client.EDCDataReport(request, para.BarCodeUserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        public Return GetDieSitInWafer(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var request = new RequestDieSiteWafer();
                request.LOTID = para.LotId;
                request.DCBLIST = new List<DCB>();
                foreach (var item in para.DcbList)
                {
                    var dcb = new Models.DCB();
                    dcb.DCBID = item.DcbID;
                    dcb.CARRIERID = item.CARRIERID;
                    dcb.CARRIERSITE = item.CARRIERSITE;
                    dcb.TOOLID = item.TOOLID;
                    dcb.DIELIST = new List<DIE>();
                    foreach (var dieitem in item.DieList)
                    {
                        var die = new Models.DIE();
                        die.DCBSITE = dieitem.DcbSite;
                        die.DIEID = dieitem.DieID;
                        die.WAFERID = dieitem.WaferID;
                        dcb.DIELIST.Add(die);
                    }
                    request.DCBLIST.Add(dcb);
                }
                var client = new SMECMESClient();
                var response = client.GetDieSitInWafer(request, para.UserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// die测试数据上报
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public Return GetBinRerult(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var request = new RequestBinDataReport();
                request.LOTID = para.LotId;
                request.PRODLIST = new List<PROD>();
                foreach (var item in para.PRODLIST)
                {
                    var prodlist = new PROD();
                    prodlist.PRODID = item.PRODID;
                    prodlist.PRODTYPE = item.PRODTYPE;
                    prodlist.BINLIST = new List<BIN>();
                    var binlst = new BIN();
                    foreach (var bin in item.BINLIST)
                    {
                        binlst.BINNAME = bin.BINNAME;
                        binlst.BINQTY = bin.BINQTY;
                        prodlist.BINLIST.Add(binlst);
                    }
                    request.PRODLIST.Add(prodlist);

                }
                var client = new SMECMESClient();
                var response = client.GetBinRerult(request, para.UserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// 采集数据上报MES 
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public Return CheckCardSlotAndMaterial(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var request = new RequestCheckCardSlotAndMaterial();
                request.EQUIPMENTID = para.EqpId;
                request.LOTID = para.LotId;
                request.CHECKLISTINFO = new List<Models.CHECKLISTINFO>();

                foreach (var localItem in para.CheckListInfo)
                {
                    var item = new Models.CHECKLISTINFO();
                    item.CARDSLOT = localItem.CARDSLOT;
                    item.FEEDERID = localItem.FEEDERID;
                    item.MATERIALLOT = localItem.MATERIALLOT;
                    request.CHECKLISTINFO.Add(item);
                }
                var client = new SMECMESClient();
                var response = client.CheckCardSlotAndMaterial(request, para.BarCodeUserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        public Return GetAllSite(TagObject para)
        {
            Return _return = new Return();
            try
            {
                var request = new RequesGetAllSite();
                request.LOTID = para.LotId;
                request.GetAllList = new List<GetAllList>();
                foreach (var localItem in para.GetAllSite)
                {
                    var item = new Models.GetAllList();
                    item.CARRIERID = localItem.CARRIERID;
                    item.CARRIERSITE = localItem.CARRIERSITE;
                    item.DOWNDCBID = localItem.DOWNDCBID;
                    item.DOWNTOOLID = localItem.DOWNTOOLID;
                    item.FRAMEID = localItem.FRAMEID;
                    item.UPDCBID = localItem.UPDCBID;
                    item.UPTOOLID = localItem.UPTOOLID;
                    request.GetAllList.Add(item);
                }
                var client = new SMECMESClient();
                var response = client.GetAllSite(request, para.BarCodeUserId);
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
        /// <summary>
        /// RMS 同步MES用户名
        /// </summary>
        /// <returns></returns>
        public Return GetSynMesUser(ref UserInfo user)
        {
            Return _return = new Return();
            try
            {
                var client = new SMECMESClient();
                var response = client.GetSynMesUser();
                user.USERTLIST = response.USERTLIST;
                if (!response.success)
                {
                    _return.SetReturnNG(response.msg);
                }
                else
                    _return.SetReturnOK();
            }
            catch (Exception ex)
            {
                _return.SetReturnNG(ex.Message);
            }
            return _return;
        }
    }
}
